package com.spn.blog.api.config;

import com.spn.blog.api.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
// 开启校验
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private AuthenticationConfiguration authenticationConfiguration;
    @Bean
    public AuthenticationManager authenticationManager() throws Exception{
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // 这里遇到 找不到 HttpSecurity，发现导入的包是阿里源的包
        http
                // 关闭csrf
                .csrf().disable()
                // 不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 未登录才能访问
                .antMatchers("/api/login").anonymous()
                // 需要认证
                .antMatchers(
                        "/api/articles/moveArticleToES",
                        "/api/articles/islike/**",
                        "/api/articles/publish",
                        "/api/articles/delete/**",
                        "/api/comments/create/change",
                        "/api/upload",
                        "/api/systeminfo",
                        "/api/users/currentUser",
                        "/api/users/currentUserDetail",
                        "/api/articles/getArticleReview/**",
                        "/api/users/upload",
                        "/api/users/getAllFriends",
                        "/api/users/getHistoryMsg").authenticated()
                // 除以上外的所有请求都不需要鉴权认证
                .anyRequest().permitAll();
        // 把认证过滤器放在第一位过滤链
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        http.cors();
        return http.build();
    }
}